#include "polygondialog.h"
#include "ui_polygondialog.h"

#include <QSqlRecord>
#include <QDebug>
#include <QMessageBox>

PolygonDialog::PolygonDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PolygonDialog)
{
    ui->setupUi(this);
    _idPolygon = -1;

    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectedItemChanged(int)));
}

PolygonDialog::~PolygonDialog()
{
    delete ui;
}

void PolygonDialog::loadPolygons()
{
    DbFabric fabric;
    QSharedPointer<QSqlRelationalTableModelWithReconnection> polygonModel = fabric.createPolygonModel();
    polygonModel->select();

    for(int i = 0; i < polygonModel->rowCount(); i ++)
    {
        QSqlRecord record = polygonModel->record(i);
        ui->comboBox->addItem(record.value("description").toString(), record.value("idPolygon"));
    }
    ui->comboBox->setCurrentIndex(0);
}

void PolygonDialog::onSelectedItemChanged(int index)
{
    QVariant data = ui->comboBox->itemData(index);
    _idPolygon = data.toString();
}

QString PolygonDialog::getPolygon()
{
    return _idPolygon;
}
